package com.cheer.ba.view.activity;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.bigkoo.svprogresshud.SVProgressHUD;
import com.cheer.ba.R;
import com.cheer.ba.constant.CodeStatus;
import com.cheer.ba.constant.ConstantTag;
import com.cheer.ba.constant.Constants;
import com.cheer.ba.model.PointModel;
import com.cheer.ba.presenter.impl.CloudBookPresenter;
import com.cheer.ba.presenter.impl.HomePresenter;
import com.cheer.ba.utils.Currency;
import com.cheer.ba.utils.StringUtils;
import com.cheer.ba.utils.ToastUtil;
import com.cheer.ba.view.base.BaseCommAty;
import com.cheer.ba.view.base.CommonAdapter;
import com.cheer.ba.view.base.ViewHolder;
import com.cheer.ba.widget.swiperefresh.SwipyRefreshLayout;
import com.cheer.ba.widget.swiperefresh.SwipyRefreshLayoutDirection;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 描述 ： 我的积分
 * 作者 ：hq
 * 时间 ：2017/5/24 15:29
 */
public class MyPointAty extends BaseCommAty implements SwipyRefreshLayout.OnRefreshListener {

    //总积分
    TextView mTxtTotalPoint;

    @BindView(R.id.id_point_refresh)
    SwipyRefreshLayout mRefreshLayout;

    @BindView(R.id.id_point_listv)
    ListView mListView;
    private CommonAdapter<PointModel> mAdapter;
    private List<PointModel> mCloudBooks = new ArrayList<PointModel>();

    private HomePresenter mHomePresenter;

    @Override
    public View loadView(LayoutInflater inflater, View childView) {
        return super.loadView(inflater, inflater.inflate(R.layout.aty_my_point, null, false));
    }

    @Override
    public boolean hasDataInPage() {
        return true;
    }

    @Override
    public void initPresenter() {
        presenter = new CloudBookPresenter();
        mHomePresenter = new HomePresenter();
        mHomePresenter.attachView(this);
    }

    @Override
    public void initView() {
        setTitleText(getString(R.string.mine_oil_point));
        // 设置刷新时动画的颜色，可以设置4个
        mRefreshLayout.setColorSchemeColors(Constants.COLOR_SCHEMES);
        mRefreshLayout.setOnRefreshListener(this);
        mRefreshLayout.setDirection(SwipyRefreshLayoutDirection.BOTH);
        initHeadView();
    }

    /**
     * 初始化头布局
     */
    private void initHeadView() {
        View mHeadView = getLayoutInflater().inflate(R.layout.head_my_point, null);
        mTxtTotalPoint = (TextView) mHeadView.findViewById(R.id.id_total_point_txt);
        mHeadView.findViewById(R.id.id_comein_point_txt).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //进入积分商城
                SVProgressHUD.showInfoWithStatus(aty, "商城正在开发，尽情期待");
            }
        });
        mListView.addHeaderView(mHeadView);
    }

    @Override
    public void initData() {
        mAdapter = new CommonAdapter<PointModel>(aty, mCloudBooks, R.layout.adp_my_point) {
            @Override
            public void convert(final ViewHolder helper, PointModel item) {
                if (null != item) {
                    if (item.scoreType == 1) {
                        helper.setText(R.id.id_title_txt, "签到");
                    } else {
                        helper.setText(R.id.id_title_txt, "消息");
                    }
                    helper.setText(R.id.id_time_txt, StringUtils.stampToDate(item.createTime, "yyyy-MM-dd HH:mm:ss"));
                    TextView mTxt = (TextView) helper.getViewById(R.id.id_point_txt);
                    if (item.logType == 1) {
                        //进
                        mTxt.setTextColor(getResources().getColor(R.color.color_main));
                        mTxt.setText("+" + Currency.returnDollar(Currency.RMB, item.score, 1));
                    } else {
                        //出
                        mTxt.setTextColor(getResources().getColor(R.color.color_333333));
                        mTxt.setText("-" + Currency.returnDollar(Currency.RMB, item.score, 1));
                    }
                }
            }
        };
        mListView.setAdapter(mAdapter);
        refreshData();
    }

    /**
     * 刷新数据
     */
    public void refreshData() {
        if (hasDataInPage()) {
            mRefreshLayout.post(new Runnable() {
                @Override
                public void run() {
                    mRefreshLayout.setRefreshing(true);
                }
            });
        }
        ((CloudBookPresenter) presenter).getMyPoints(false);
        //获取我的总积分
        mHomePresenter.getMyTotalPoint();
    }

    @Override
    public <T> void updateViewWithTag(T t, String tag) {
        if (null != t) {
            if (TextUtils.equals(tag, ConstantTag.GET_POINT_SUCCESS)) {
                //获取总积分成功
                PointModel model = (PointModel) t;
                mTxtTotalPoint.setText(Currency.returnDollar(Currency.RMB, model.score, 1));
            } else if (TextUtils.equals(tag, ConstantTag.GET_POINT_FAIL)) {
                //获取总积分失败
                mTxtTotalPoint.setText("- -");
            }
        }
    }

    @Override
    public <T> void updateViewWithLoadMore(T t, boolean loadMore, String tag) {
        if (null != t) {
            if (TextUtils.equals(ConstantTag.GET_LIST_SUCCESS, tag)) {
                mRefreshLayout.setRefreshing(false);
                List<PointModel> books = (List<PointModel>) t;
                if (books.isEmpty()) {
                    if (loadMore) {
                        ToastUtil.showToast(getString(R.string.no_more_data));
                    } else {
                        mAdapter.notifyDataSetChanged();
                    }
                } else {
                    if (!loadMore) {
                        mCloudBooks.clear();
                    }
                    mCloudBooks.addAll(books);
                    mAdapter.notifyDataSetChanged();
                }
            }
        }
    }

    @Override
    public void handlerByPage(int flag1, int flag2, boolean loadMore) {
        mRefreshLayout.setRefreshing(false);
        if (flag1 == CodeStatus.Gegeneral.ERROR_NET) {
            if (loadMore) {
                ToastUtil.showToast("无网络");
            } else {
                mAdapter.notifyDataSetChanged();
            }
        } else if (flag1 == CodeStatus.Gegeneral.ERROR_DATA) {
            if (loadMore) {
                ToastUtil.showToast("数据加载失败");
            } else {
                mAdapter.notifyDataSetChanged();
            }
        } else if (flag1 == CodeStatus.Gegeneral.DATA_SUCCESS_FULL) {

        }
    }

    @Override
    public void onRefresh(SwipyRefreshLayoutDirection direction) {
        ((CloudBookPresenter) presenter).getMyPoints(direction != SwipyRefreshLayoutDirection.TOP);
    }
}
